package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.service.rest.usercenter.ResourceRestService;
import com.yenlien.traceyun.webase.controller.BaseController;
import com.yenlien.traceyun.webase.exception.BusinessException;
import com.yenlien.traceyun.webase.utils.CheckParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LiuSha on 2020/8/14.
 */
@RestController
@RequestMapping("/resource")
public class ResourceController extends BaseController {
    @Autowired
    private ResourceRestService resourceRestService;

    /**
     * 加载app对应的资源
     *
     * @return
     */
    @RequestMapping(value = "/findByAppId/{appId}", method = RequestMethod.GET)
    public Object findByAppId(@PathVariable("appId") String appId) {
        return resourceRestService.findByAppId(appId);
    }

    /**
     * 新增app对应的资源
     *
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody JSONObject jsonObject) {
        return resourceRestService.save(jsonObject);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/dele/{id}")
    public Object dele(@PathVariable("id") String id) {
        return resourceRestService.dele(id);
    }

    /**
     * 加载用户所有app的资源
     *
     * @return
     */
    @GetMapping(value = "/load/user/appmenus/{userId}")
    public Object loadAllAppMenu(@PathVariable("userId") String userId) {
        return resourceRestService.loadAllAppMenu(userId);
    }

    /**
     * 加载用户所有app的资源
     *
     * @return
     */
    @GetMapping(value = "/load/company/appmenus/{companyId}")
    public Object loadAllCompanyAppMenu(@PathVariable("companyId") String companyId) {
        return resourceRestService.loadAllCompanyAppMenu(companyId);
    }

    @PostMapping(value = "/set/company/appmenus")
    public Object setCompanyAppmenus(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"companyId"});
        return resourceRestService.setCompanyAppMenu(params);
    }


    @PostMapping(value = "/set/user/appmenus")
    public Object setUserAppmenus(@RequestBody JSONObject params) {
        CheckParams.checkJson(params, new String[]{"userId"});
        return resourceRestService.setUserAppMenu(params);
    }
}
